package com.imon.jmxbox;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.MapMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.JmsUtils;
import org.springframework.stereotype.Component;

@Component
public class SpringBackOfficeImpl implements BackOffice {
	private static Log LOG = LogFactory.getLog(SpringBackOfficeImpl.class);
	@Resource
	private JmsTemplate jmsTemplate;

	public Mail receiveMail() {
		LOG.debug("Received message from JmsTemplate");
		MapMessage message = (MapMessage) jmsTemplate.receive();
		if (message == null)
			return null;

		try {
			return new Mail(message.getString("mailId"),
					message.getString("country"), message.getDouble("weight"));
		} catch (JMSException e) {
			throw JmsUtils.convertJmsAccessException(e);
		}
	}
}
